﻿using System;
using System.Web.Security;
using KLS.PPDS.Utilities.Enums;
using KLS.PPDS.ApplicationService.Manager;




namespace KLS.PPDS.ApplicationService.Provider
{
    public class CustomMembershipProvider : System.Web.Security.MembershipProvider
    {
        public override string ApplicationName
        {
            get { throw new NotImplementedException(); }
            set { throw new NotImplementedException(); }
        }

        public override bool ChangePassword(string username, string oldPassword, string newPassword)
        {
            throw new NotImplementedException();
        }

        public override bool ChangePasswordQuestionAndAnswer(string username, string password, string newPasswordQuestion, string newPasswordAnswer)
        {
            throw new NotImplementedException();
        }

        public override System.Web.Security.MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out System.Web.Security.MembershipCreateStatus status)
        {
            throw new NotImplementedException();
        }

        public override bool DeleteUser(string username, bool deleteAllRelatedData)
        {
            throw new NotImplementedException();
        }

        public override bool EnablePasswordReset
        {
            get { throw new NotImplementedException(); }
        }

        public override bool EnablePasswordRetrieval
        {
            get { throw new NotImplementedException(); }
        }

        public override System.Web.Security.MembershipUserCollection FindUsersByEmail(string emailToMatch, int pageIndex, int pageSize, out int totalRecords)
        {
            throw new NotImplementedException();
        }

        public override System.Web.Security.MembershipUserCollection FindUsersByName(string usernameToMatch, int pageIndex, int pageSize, out int totalRecords)
        {
            throw new NotImplementedException();
        }

        public override System.Web.Security.MembershipUserCollection GetAllUsers(int pageIndex, int pageSize, out int totalRecords)
        {
            throw new NotImplementedException();
        }

        public override int GetNumberOfUsersOnline()
        {
            throw new NotImplementedException();
        }

        public override string GetPassword(string username, string answer)
        {
            throw new NotImplementedException();
        }

        public override System.Web.Security.MembershipUser GetUser(string username, bool userIsOnline)
        {
            throw new NotImplementedException();
        }

        public override System.Web.Security.MembershipUser GetUser(object providerUserKey, bool userIsOnline)
        {
            throw new NotImplementedException();
        }

        public override string GetUserNameByEmail(string email)
        {
            throw new NotImplementedException();
        }

        public override int MaxInvalidPasswordAttempts
        {
            get { throw new NotImplementedException(); }
        }

        public override int MinRequiredNonAlphanumericCharacters
        {
            get { throw new NotImplementedException(); }
        }

        public override int MinRequiredPasswordLength
        {
            get { throw new NotImplementedException(); }
        }

        public override int PasswordAttemptWindow
        {
            get { throw new NotImplementedException(); }
        }

        public override System.Web.Security.MembershipPasswordFormat PasswordFormat
        {
            get { throw new NotImplementedException(); }
        }

        public override string PasswordStrengthRegularExpression
        {
            get { throw new NotImplementedException(); }
        }

        public override bool RequiresQuestionAndAnswer
        {
            get { throw new NotImplementedException(); }
        }

        public override bool RequiresUniqueEmail
        {
            get { throw new NotImplementedException(); }
        }

        public override string ResetPassword(string username, string answer)
        {
            throw new NotImplementedException();
        }

        public override bool UnlockUser(string userName)
        {
            throw new NotImplementedException();
        }

        public override void UpdateUser(System.Web.Security.MembershipUser user)
        {
            throw new NotImplementedException();
        }

        public override bool ValidateUser(string username, string password)
        {
            var u = UserManager.Get(username);
            
            //if (u != null)
            //{
            //    if (u.Password == password)
            //    {
            //        var dep = new Department();
            //        Language lang = null;
            //        var userType = GetUserType(u.UserName);
            //        var isSuperAdmin = userType.Equals(UserType.SuperAdmin);
            //        var companyNo = string.Empty;
            //        Company userComp = null;
            //        UserGroupRel ugr = u.Groups.Where(g => g != null && g.Group!=null).FirstOrDefault();
            //        var hasAnyGroup = ugr != null && ugr.Group!=null ? true: false;
            //        if(!isSuperAdmin)
            //        {
            //            companyNo = u.CompanyNo;
            //            userComp = CompanyManager.GetCompanyByNo(u.CompanyNo);
            //        }
                  
            //        try
            //        {
            //            dep = DepartmentManager.GetDepartmentByDepartmentNumber(u.DepartmentId, companyNo);
            //            if (dep!=null)
            //            lang = LanguageManager.Get(dep.Language);
            //        }
            //        catch
            //        {
                        
            //        }


            //        var user = new KLS.PPDS.Utilities.KLS.PPDSUser
            //                       {
            //                           Name = u.UserName,
            //                           Id = u.UserId,
            //                           FullName = u.UserFullName,
            //                           CompanyId = isSuperAdmin
            //                                           ? 0
            //                                           : userComp != null && !userComp.Deleted
            //                                                 ? userComp.CompanyId
            //                                                 : 0,
            //                           CompanyNo = companyNo,
            //                           LanguageId = u.LanguageId.HasValue
            //                                            ? u.LanguageId.Value : isSuperAdmin ? 0
            //                                            : lang != null
            //                                                  ? lang.LanguageId
            //                                                  : (userComp!=null
            //                                                         ? userComp.LanguageId
            //                                                         : 0),
            //                           DepartmentId = u.DepartmentId,
            //                           CompanyName = isSuperAdmin
            //                                             ? string.Empty
            //                                             : userComp!=null
            //                                                   ? userComp.CompanyName
            //                                                   : string.Empty,
            //                           UserType = userType,
            //                           Validated = true,
            //                           IsSuperAdmin = isSuperAdmin,
            //                           HasAnyGroup = hasAnyGroup,
            //                           Deleted = u.Deleted,
            //                           KLS.PPDSsettings = KLS.PPDSSettingManager.GetAllKLS.PPDSSetting()

                                       
            //                       };

            //        var prop = PropertyManager.Get(Convert.ToString(ScopeName.Company), user.CompanyId.ToString(),
            //                            "/module.information/documents/remotepath");
            //        var defaultVal = KLS.PPDSConfig.Instance.PropertyConfigMap["/module.information/documents/remotepath"]!=null ? KLS.PPDSConfig.Instance.PropertyConfigMap["/module.information/documents/remotepath"].Default : string.Empty;
            //        user.CompanyRemotePath = defaultVal;
            //        if(prop!=null)
            //        {
            //            var propValue = prop.PropertyValueList.ToList().Find(x => x.ScopeId.Equals(user.CompanyId));
            //            if (propValue != null)
            //            {
            //                user.CompanyRemotePath = propValue.Value;
            //            }
            //        }
            //        KLS.PPDSContext.CurrentUser = user;
             //       return true;
               // }
           // }
            return false;
        }

        private UserType GetUserType(string userName)
        {
            if (userName.Equals("admin", StringComparison.OrdinalIgnoreCase))
            {
                return UserType.SuperAdmin;
            }
            else if (Roles.Provider.IsUserInRole(userName, "/KLS.PPDS.Administration/Users/View/"))
            {
                return UserType.CompanyAdmin;
            }
            else
            {
                return UserType.User;
            }

        }
    }
}
